﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using RoadTeam.SysInterface;
using RoadTeam.Pub;
using RoadTeam.Model;
using RoadTeam.Enums;

namespace RoadTeam.BLL
{
    public class ValidatePro2Bll
    {
        private static  object o =new object ();

        public DataTable GetValidatePro2DataByPage(String OrganIdentifier, int CurrentPage, int PageSize)
        {
            String OrganId = FactoryClass.CreateSmOrgan().GetOraganID(OrganIdentifier);
            int AllPageCount = FactoryClass.CreateCsValidatePro().GetValidateProDataAllPageCount(OrganId);
            DataTable Rt_Prodt = FactoryClass.CreateCsValidatePro().GetValidateProDataByPage(OrganId, PageSize, CurrentPage, AllPageCount);
            return SyncValidateProData(Rt_Prodt);
        }

        public DataRow GetValidateProRow(String ValidateProIdentifier, String DoRoomRela, String RoObjectRela)
        {
            SqlWhere where = new SqlWhere();
            where.Info = new CsValidatePro2() { ValidateProIdentifier = ValidateProIdentifier, DoRoomRela = DoRoomRela, RoObjectRela = RoObjectRela };
            DataTable dt = CsValidatePro2.Select(where, GlobalConfig.ConnectionString);
            if (dt.Rows.Count == 0) return null;
            return dt.Rows[0];
        }



        private DataTable SyncValidateProData(DataTable Rt_Prodt)
        {
            //更新验房户型,验房房间类型,验房对象信息
            SyncValidateProOtherData(Rt_Prodt);
            #region 方法不可用
            //List<String> liProIdentifier = new List<string>();
            //String ProIdentifiers = @"'-1'";
            //foreach (DataRow dr in Rt_Prodt.Rows)
            //{
            //    if (dr["ValidateProIdentifier"] != null && !String.IsNullOrEmpty(dr["ValidateProIdentifier"].ToString()))
            //    {
            //        ProIdentifiers += ",'" + dr["ValidateProIdentifier"].ToString() + "'";
            //        liProIdentifier.Add(dr["ValidateProIdentifier"].ToString());
            //    }
            //}
            //DataTable dt_Pro = GetProData(ProIdentifiers);
            //DataRow[] drs_Pro;
            //drs_Pro = Rt_Prodt.Select(@" ValidateProIdentifier IS NULL ");
            //if (drs_Pro.Count() != 0)
            //{
            //    InsertValidatePro(Rt_Prodt, dt_Pro);
            //}

            //if (liProIdentifier.Count > 0)
            //{
            //    drs_Pro = Rt_Prodt.Select(@" ValidateProIdentifier IN (" + ProIdentifiers + ")");
            //    UpdateValidatePro(ConverTable.ToDataTable(drs_Pro), dt_Pro);
            //}
            //Rt_Prodt.Columns["ValidateProIdentifier"].ColumnName = @"App_Identifier";
            //FactoryClass.CreateCsValidatePro().UpdateValidateProTable(Rt_Prodt);
            //CsValidatePro2.InsertDatatable(dt_Pro, GlobalConfig.ConnectionString);
            //CsValidatePro2.Update(dt_Pro, GlobalConfig.ConnectionString);
            #endregion

            DataTable dt_Pro = GetProData("-1");
            DataRow drs;

            foreach (DataRow dr in Rt_Prodt.Rows)
            {
                drs = GetValidateProRow(dr["ValidateProIdentifier"].ToString(), dr["DoRoomRela"].ToString(), dr["RoObjectRela"].ToString());
                if (drs != null)
                {

                    //修改算法
                    InsertValidatePro(dr, drs);
                    dt_Pro.ImportRow(drs);
                }
                else
                {

                    //新增算法
                    drs = dt_Pro.NewRow();
                    InsertValidatePro(dr, drs);
                    dt_Pro.Rows.Add(drs);
                }
            }
            CsValidatePro2.InsertDatatable(dt_Pro, GlobalConfig.ConnectionString);
            CsValidatePro2.Update(dt_Pro, GlobalConfig.ConnectionString);
            return dt_Pro;
        }

        #region 同步修改方法[不可用]
        //private void UpdateValidatePro(DataTable Rt_Pro, DataTable dt_Pro)
        //{
        //    String Rtidentity = @"ValidateProIdentifier";
        //    String idtntity = @"ValidateProIdentifier";
        //    String EnmunType = @"ValidateProColumnDictionary";
        //    RoadTeam.Pub.SyncTable.SyncDataTable(Rt_Pro, dt_Pro, Rtidentity, idtntity, EnmunType);
        //    CsValidatePro2.Update(dt_Pro, GlobalConfig.ConnectionString);
        //}

        #endregion

        #region 新增插入的ValidatePro
        private void InsertValidatePro(DataRow Rt_Prodr, DataRow dr_Pro)
        {
            foreach (DataColumn Rt_Dc in Rt_Prodr.Table.Columns)
            {
                foreach (DataColumn Dc in dr_Pro.Table.Columns)
                {
                    if (Rt_Dc.ColumnName.ToLower() == Dc.ColumnName.ToLower())
                    {
                        dr_Pro[Dc.ColumnName.ToLower()] = Rt_Prodr[Rt_Dc.ColumnName.ToLower()];
                    }
                }
            }
        }
        #endregion

        #region 更新验房户型,验房房间类型,验房对象信息
        private void SyncValidateProOtherData(DataTable Rt_Prodt)
        {
            #region 更新户型信息
            List<string> li = new List<string>();
            String StrIn = String.Empty;
            foreach (DataRow dr in Rt_Prodt.Rows)
            {
                if (dr["DoorTypeID"] != null && !li.Contains(dr["DoorTypeID"].ToString()))
                {
                    li.Add(dr["DoorTypeID"].ToString());
                    StrIn += "'" + dr["DoorTypeID"].ToString() + "',";
                }
            }
            DataTable dt_DoorType = FactoryClass.CreateCsDoorType().GetDoorTypeData(StrIn.TrimEnd(','));
            DataRow dr_DoorType;
            foreach (DataRow dr in Rt_Prodt.Rows)
            {
                dr_DoorType = dt_DoorType.Select(@"DoorTypeID=" + dr["DoorTypeID"].ToString())[0];
                if (dr_DoorType["App_Identifier"] == null ||
                    String.IsNullOrEmpty(dr_DoorType["App_Identifier"].ToString()))
                {
                    dr_DoorType["App_Identifier"] = CreateIdentifier.GetIdentifier();
                    dr["DoorTypeIdentifier"] = dr_DoorType["App_Identifier"];
                }
                else
                {
                    dr["DoorTypeIdentifier"] = dr_DoorType["App_Identifier"];
                }
            }
            FactoryClass.CreateCsDoorType().UpdateDoorTypeTable(dt_DoorType);
            #endregion

            #region 更新房间类型信息
            li = new List<string>();
            StrIn = String.Empty;
            foreach (DataRow dr in Rt_Prodt.Rows)
            {
                if (dr["RoomTypeID"] != null && !li.Contains(dr["RoomTypeID"].ToString()))
                {
                    li.Add(dr["RoomTypeID"].ToString());
                    StrIn += "'" + dr["RoomTypeID"].ToString() + "',";
                }
            }
            DataTable dt_RoomType = FactoryClass.CreateCsRoomType().GetRoomTypeData(StrIn.TrimEnd(','));
            DataRow dr_RoomType;
            foreach (DataRow dr in Rt_Prodt.Rows)
            {
                dr_RoomType = dt_RoomType.Select(@"RoomTypeID=" + dr["RoomTypeID"].ToString())[0];
                if (dr_RoomType["App_Identifier"] == null ||
                    String.IsNullOrEmpty(dr_RoomType["App_Identifier"].ToString()))
                {
                    dr_RoomType["App_Identifier"] = CreateIdentifier.GetIdentifier();
                    dr["RoomTypeIdentifier"] = dr_RoomType["App_Identifier"];
                }
                else
                {
                    dr["RoomTypeIdentifier"] = dr_RoomType["App_Identifier"];
                }
            }
            FactoryClass.CreateCsRoomType().UpdateRoomTypeTable(dt_RoomType);
            #endregion

            #region 更新验房对象信息
            li = new List<string>();
            StrIn = String.Empty;
            foreach (DataRow dr in Rt_Prodt.Rows)
            {
                if (dr["ValidateObjectID"] != null && !li.Contains(dr["ValidateObjectID"].ToString()))
                {
                    li.Add(dr["ValidateObjectID"].ToString());
                    StrIn += "'" + dr["ValidateObjectID"].ToString() + "',";
                }
            }
            DataTable dt_ValidateObject = FactoryClass.CreateCsValidateObject().GetValidateObjectData(StrIn.TrimEnd(','));
            DataRow dr_ValidateObject;
            foreach (DataRow dr in Rt_Prodt.Rows)
            {
                dr_ValidateObject = dt_ValidateObject.Select(@"ValidateObjectID=" + dr["ValidateObjectID"].ToString())[0];
                if (dr_ValidateObject["App_Identifier"] == null ||
                    String.IsNullOrEmpty(dr_ValidateObject["App_Identifier"].ToString()))
                {
                    dr_ValidateObject["App_Identifier"] = CreateIdentifier.GetIdentifier();
                    dr["ValidateObjectIdentifier"] = dr_ValidateObject["App_Identifier"];
                }
                else
                {
                    dr["ValidateObjectIdentifier"] = dr_ValidateObject["App_Identifier"];
                }
            }
            FactoryClass.CreateCsValidateObject().UpdateValidateObjectTable(dt_ValidateObject);
            #endregion

            #region 更新验房项目信息

            li = new List<string>();
            StrIn = String.Empty;
            foreach (DataRow dr in Rt_Prodt.Rows)
            {
                if (dr["ValidateProID"] != null && !li.Contains(dr["ValidateProID"].ToString()))
                {
                    li.Add(dr["ValidateProID"].ToString());
                    StrIn += "'" + dr["ValidateProID"].ToString() + "',";
                }
            }
            DataTable dt_ValidatePro = FactoryClass.CreateCsValidatePro().GetValidateProDataByIDs(StrIn.TrimEnd(','));
            DataRow dr_ValidatePro;

            foreach (DataRow dr in Rt_Prodt.Rows)
            {
                dr_ValidatePro = dt_ValidatePro.Select(@"ValidateProID=" + dr["ValidateProID"].ToString())[0];
                if (dr_ValidatePro["App_Identifier"] == null ||
                    String.IsNullOrEmpty(dr_ValidatePro["App_Identifier"].ToString()))
                {
                    dr_ValidatePro["App_Identifier"] = CreateIdentifier.GetIdentifier();
                    dr["ValidateProIdentifier"] = dr_ValidatePro["App_Identifier"];
                }
                else
                {
                    dr["ValidateProIdentifier"] = dr_ValidatePro["App_Identifier"];
                }
            }
            FactoryClass.CreateCsValidatePro().UpdateValidateProTable(dt_ValidatePro);



            #endregion


        }
        #endregion
        public DataTable GetProData(String ProIdentifiers)
        {
            SqlWhere where = new SqlWhere();
            where.StrSqlWhere = @"  ValidateProIdentifier IN ('" + ProIdentifiers + "') ";
            return CsValidatePro2.Select(where, GlobalConfig.ConnectionString);
        }



        #region 获取验房项目的总条数
        /// <summary>
        /// 获取验房项目的总条数
        /// </summary>
        /// <param name="OrganIdentifier">机构标识</param>
        /// <returns>总条数</returns>
        public int GetValidateProDataAllPageCount(String OrganIdentifier)
        {
            String OrganID = FactoryClass.CreateSmOrgan().GetOraganID(OrganIdentifier);
            return FactoryClass.CreateCsValidatePro().GetValidateProDataAllPageCount(OrganID);
        }
        #endregion


    }
}
